package com.grm.controller;


import com.grm.common.Result;
import com.grm.config.MqttGateway;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 模拟给mqtt发送消息
 *
 * @author gaorimao
 * @date 2022/04/13
 */
@RestController
@RequestMapping("/mqtt")
public class MqttController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private MqttGateway mqttGateway;

    /**
     * 发送mqtt消息
     *
     * @param qos     消息质量
     * @param topic   消息主题
     * @param message 消息内容
     * @return {@link Result}
     */
    @GetMapping("/send")
    public Result send(@RequestParam int qos, @RequestParam String topic, @RequestParam String message) {
        mqttGateway.sendMessageToMqtt(topic, qos, message);
        this.logger.info("发送mqtt消息完成,主题：{},质量：{},消息：{}", topic, qos, message);
        return Result.success("发送mqtt消息完成");
    }
}